package com.oneplatform.permission.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mendmix.common.annotation.ApiMetadata;
import com.mendmix.common.constants.PermissionLevel;
import com.mendmix.common.model.IdParam;
import com.mendmix.common.model.Page;
import com.mendmix.common.model.PageParams;
import com.mendmix.common.model.PageQueryRequest;
import com.mendmix.common.model.SelectOption;
import com.mendmix.common.model.TreeModel;
import com.oneplatform.permission.dao.entity.ApiResourceEntity;
import com.oneplatform.permission.dto.ApiResource;
import com.oneplatform.permission.dto.BizSystemModule;
import com.oneplatform.permission.dto.param.ApiResourceParam;
import com.oneplatform.permission.dto.param.ApiResourceQueryParam;
import com.oneplatform.permission.service.ApiResourceService;
import com.oneplatform.permission.service.BizSystemService;


@RestController("AppApiResourceController")
@RequestMapping("/apiResource")
public class ApiResourceController {

    private @Autowired ApiResourceService apiResourceService;
    private @Autowired BizSystemService systemService;

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @PostMapping(value = "add")
    @ResponseBody
    public IdParam<Integer> add(@RequestBody ApiResourceParam param) {
        return apiResourceService.addApiResource(param);
    }

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @PostMapping(value = "delete")
    @ResponseBody
    public void delete(@RequestBody IdParam<Integer> param) {
        apiResourceService.deleteApiResource(param.getId());
    }

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @PostMapping("update")
    @ResponseBody
    public void update(@RequestBody ApiResourceParam param) {
        apiResourceService.updateApiResource(param);
    }

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @PostMapping("toggle")
    @ResponseBody
    public void toggleApi(@RequestBody IdParam<Integer> param) {
        apiResourceService.switchApiResource(param.getId());
    }

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @GetMapping(value = "{id}")
    @ResponseBody
    public ApiResource get(Integer id) {
        return apiResourceService.getApiResourceById(id);
    }

    
    @ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired,actionLog = true)
    @PostMapping("list")
    @ResponseBody
    public Page<ApiResource> pageQry(@RequestBody PageQueryRequest<ApiResourceQueryParam> queryParam) {
        return apiResourceService.pageQry(new PageParams(queryParam.getPageNo(),queryParam.getPageSize()),queryParam.getExample());
    }

    
    @GetMapping("options")
    @ResponseBody
    public List<SelectOption> options(@RequestParam(name="moduleId",required=false) Integer moduleId) {
    	List<ApiResourceEntity> list = apiResourceService.findByModuleId(moduleId);
    	return list.stream().map(e -> {
            return new SelectOption(e.getId().toString(),e.getName());
        }).collect(Collectors.toList());
    }

    @ApiMetadata(permissionLevel = PermissionLevel.LoginRequired)
    @GetMapping(value = "/tree")
    @ResponseBody
    public List<TreeModel> apiResourceTree() {
    	ApiResourceQueryParam queryParam = new ApiResourceQueryParam();
    	queryParam.setEnabled(true);
    	queryParam.setGrantType(PermissionLevel.PermissionRequired.name());
    	List<TreeModel> models = apiResourceService.listByQueryParam(queryParam).stream().map(o -> {
    		String pid = o.getModuleId().toString();
    		return new TreeModel(o.getId().toString(), o.getName(), o.getUri(), pid); 
    	}).collect(Collectors.toList());
    	//
    	List<BizSystemModule> modules = systemService.getSystemModules();
    	TreeModel model;
    	for (BizSystemModule module : modules) {
    		model = new TreeModel(module.getId().toString(), module.getName(), module.getServiceId(), null);
    		models.add(model);
		}
    	
		return TreeModel.build(models).getChildren();
    }

}
